<?php

class UpdateMeta {
	private $api_obj;
	private $type;
	private $obj_id;
	private $obj;
	private $user;
	private $source_id;
	private $en_lang;
	private $cn_lang;
	private $get_system_tags=false;

	public function __construct($type,$obj_id=null){
		if(! ($type=strtolower(trim($type))) ){
			//throw new Exception('$type must be "Image" or "Video"');
			error_log('$type must be "Image" or "Video"');
			exit();
		}
		$class = ucfirst($type) . 'InfrastructureClient';
		$this->api_obj = new $class(); 
		$this->cn_lang = language::getLanguageID(AC::LANGUAGE_ZH);
		$this->en_lang = language::getLanguageID(AC::LANGUAGE_EN);
		$this->type    = $type;
		$this->obj_id  = $obj_id;
		$this->user    = CommonUtils::get_user();

		$this->obj       = null;
		$this->source_id = null;

		if($obj_id){
			$this->obj = new $type();
			$this->obj = $this->obj->get($this->obj_id);
			$this->source_id = $this->obj->ii_id;
			if('video'==$this->type){
				$this->source_id = $this->obj->vi_id;
			}
		}
	}
	public function UpdateAcl(){
		if('video'==$this->type){

			if(empty($this->obj->vi_id) || $this->obj->vi_id<1) return false; 

			$acl_destination_1 = $acl_destination_2 = array(
				$this->obj->vi_id,
				1, // destination 
				$this->obj->created_by,
				strtotime($this->obj->publish_datetime),
				strtotime($this->obj->expire_datetime),
				explode(',',$this->obj->country_allow_list),
				explode(',',$this->obj->country_deny_list)
			);
			$acl_destination_2[1] = 2;
			call_user_func_array(array($this->api_obj,'updateAcl'),$acl_destination_1);
			call_user_func_array(array($this->api_obj,'updateAcl'),$acl_destination_2);
			return true;
		}
		return $this->api_obj->UpdateAcl(
			$this->source_id, 
			AC::II_DESTINATION_ID, 
			$this->user, 
			strtotime($this->obj->publish_datetime),
			strtotime($this->obj->expire_datetime), 
			explode(',',$this->obj->country_allow_list), 
			explode(',',$this->obj->country_deny_list)
		);
	}

	public function getSystemTags(){$this->get_system_tags=true;}

	/**
	 * System tags for Programme:
	 * Title(Chinese), Title (English)
	 */
	public function UpdateMetaProgramme($p_id=null) {
		if(empty($p_id) && !$this->get_system_tags)
		{
			$p_id = $this->obj->programme_id;
		}

		$sql = '
			 SELECT 
			(SELECT title FROM `programme-language` WHERE programme_id='.$p_id.' AND language_id='.$this->cn_lang.') as cn_name,
			(SELECT title FROM `programme-language` WHERE programme_id='.$p_id.' AND language_id='.$this->en_lang.') as en_name
		';
		$result = Model::$dbo['r']->findBySql('programme_language',$sql);
		$system_tags = array(
			'cn_title'    => isset($result[0]->cn_name) ? $result[0]->cn_name : '',
			'en_title'    => isset($result[0]->en_name) ? $result[0]->en_name : ''
		);
		if($this->get_system_tags) return $system_tags;
		
		return $this->api_obj->UpdateMetaProgramme($this->source_id, $p_id, $this->user, $system_tags);
	}

	/**
	 * System tags for Episode
	 * Title (Chinese), folder_id, folder_name
	 */
	public function UpdateMetaEpisode($episode_id) {
		$e_id  = (int)$episode_id;
		$e_obj = Model::$dbo['r']->find('episode_language','episode_id=? and language_id=?',array($e_id,$this->cn_lang));
		$system_tags = array(
			'cn_title'    => isset($e_obj[0]->title) ? $e_obj[0]->title : '',
		);
		if($this->get_system_tags) return $system_tags;
		
		return $this->api_obj->UpdateMetaEpisode($this->source_id, $e_id, $this->user, $system_tags);
	}

	/**
	 * System tags for Cast
	 * Cast Name , Cast Nickname , Real Name
	 */
	public function UpdateMetaCast($cast_id) {
		$c_id  = (int)$cast_id;
		$c_obj = Model::$dbo['r']->find('cast_language','cast_id=? and (language_id=? or language_id=?)',array($c_id,$this->cn_lang,$this->en_lang));
		if($c_obj){
			if ($c_obj[0]->language_id == 1)
			{
				$system_tags = array(
					'cast_last_name'  => $c_obj[0]->last_name,
					'cast_first_name' => $c_obj[0]->first_name,
					'nick_name'       => $c_obj[0]->nickname,
					'real_last_name'  => $c_obj[0]->real_last_name,
					'real_first_name' => $c_obj[0]->real_first_name,
				);
			}
			else
			{
				$system_tags = array(
					'cast_last_name'  => $c_obj[0]->first_name,
					'cast_first_name' => $c_obj[0]->last_name,
					'nick_name'       => $c_obj[0]->nickname,
					'real_last_name'  => $c_obj[0]->real_first_name,
					'real_first_name' => $c_obj[0]->real_last_name,
				);
			}
		}else{
			$system_tags = array(
				'cast_last_name'  => '', 
				'cast_first_name' => '', 
				'nick_name'       => '',
				'real_last_name'  => '',
				'real_first_name' => '',
			);
		}
		if($this->get_system_tags) return $system_tags;
		
		return $this->api_obj->UpdateMetaCast($this->source_id, $c_id, $this->user,$system_tags);
	}

	public function UpdateMeta($type=null,$id=null){
		// video :  UpdateMeta($source_id, $login_id, $title, $person, $tag, array $miscellaneous) 
		if('image'==$this->type){
			if(!$type){
				return false;
			}
			$function = 'UpdateMeta'.ucfirst(strtolower(trim($type)));
			$this->$function($id);
		}else{
			$vl_obj = Model::$dbo['r']->find('video_language','video_id=? and language_id=?',array($this->obj_id,$this->cn_lang));
			$this->api_obj->UpdateMeta(
				$this->source_id,
				CommonUtils::get_user(),
				isset($vl_obj[0]->title) ? $vl_obj[0]->title : '',
				$this->obj->created_by,
				$this->obj->tags,
				array()
			);
		}
	}
}
